package action;

import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.springframework.stereotype.Controller;

import service.IGoodsService;
import service.impl.GoodsServices;
import util.Result;
import vo.GoodsInfo;
import vo.VipInfo;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller
@Action(value = "goodsAction")
public class GoodsAction extends BaseAction implements ModelDriven<GoodsInfo> {
	protected IGoodsService goodsServices;
	protected GoodsInfo goodsinfo = new GoodsInfo();
	private String goodname;
	private int item;
	private int rows = 1;// 每页显示的记录数
	private int page = 20;// 当前第几页
	private int id;

	public void browseGoods() throws Exception {
		int total = goodsServices.getTotalNum();
		List<GoodsInfo> list = goodsServices.getGoods(page, rows);
		try {
			writeJson(total, list);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void addGoods() throws Exception {

		Result result = new Result();
		try {
			goodsServices.addGoods(goodsinfo);
			result.setSuccess(true);
			result.setMsg("添加客户成功");
		} catch (Exception e) {
			e.printStackTrace();
			result.setMsg("添加客户失败");
		}
		writeJson(result);
	}

	public void deleteGoods() throws Exception {
		Result result = new Result();
		try {
			GoodsInfo goodinfo = goodsServices.findGoodsById(id);
			goodsServices.deleteGoods(goodinfo);
			result.setSuccess(true);
			result.setMsg("删除客户成功");
		} catch (Exception e) {
			e.printStackTrace();
			result.setMsg("删除客户失败");
		}
		writeJson(result);
	}

	public void updateGoods() throws Exception {
		Result result = new Result();
		try {
			goodsServices.updateGoods(goodsinfo);
			result.setSuccess(true);
			result.setMsg("修改客户成功");
			result.setSuccess(true);
		} catch (Exception e) {
			result.setMsg("添加客户失败");
		}
		writeJson(result);
	}

	public String findGoodsById() throws Exception {

		goodsinfo = goodsServices.findGoodsById(goodsinfo.getGId());
		Map request = (Map) ActionContext.getContext().get("request");
		request.put("goodsinfo", goodsinfo);
		return SUCCESS;
	}

	public GoodsInfo findGoodsById(int goodid) {
		goodsinfo = goodsServices.findGoodsById(goodid);
		return goodsinfo;
	}

	public String searchBook() throws Exception {
		System.out.println(item);
		StringBuffer hql = new StringBuffer("from GoodsInfo b");
		if (goodname != null && goodname.length() != 0 && item == 0) {
			hql.append(" where b.GName like '%" + goodname + "%'");
		} else if (goodname != null && goodname.length() != 0 && item == 1) {
			hql.append(" where b.GType like '%" + goodname + "%'");
		}
		System.out.println(hql);
		List goods = goodsServices.searchGoods(hql.toString());
		Map request = (Map) ActionContext.getContext().get("request");
		request.put("goods", goods);
		return SUCCESS;

	}

	public IGoodsService getGoodsServices() {
		return goodsServices;
	}

	public void setGoodsServices(IGoodsService goodsServices) {
		this.goodsServices = goodsServices;
	}

	public GoodsInfo getGoodsinfo() {
		return goodsinfo;
	}

	public void setGoodsinfo(GoodsInfo goodsinfo) {
		this.goodsinfo = goodsinfo;
	}

	public String getGoodname() {
		return goodname;
	}

	public void setGoodname(String goodname) {
		this.goodname = goodname;
	}

	public int getItem() {
		return item;
	}

	public void setItem(int item) {
		this.item = item;
	}

	@Override
	public GoodsInfo getModel() {
		// TODO Auto-generated method stub
		return goodsinfo;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

}
